<?php
namespace app\api\controller;

use Alipay\EasySDK\Kernel\Factory;
use Alipay\EasySDK\Kernel\Config;
use Alipay\EasySDK\Kernel\Util\ResponseChecker;

/**
 * 支付宝接口
 * Class Wx
 * @package app\api\controller
 * https://github.com/alipay/alipay-easysdk/tree/master/php
 */
class PayZFB extends MY
{
    const CONFIG = [
        //支付宝应用id
        "appId"      => "",
        //支付宝应用密钥
        "appSecret"  => "",
        //支付回调地址
        "notifyUrl" => "https://www.bszy.com/api.php/pay/notifyZFB",
        //请求头设置
        "header"    => [
            "Content-Type: application/json; charset=utf-8",
            "Accept: application/json",
        ],
        //证书目录
        "payCert" => "public/static/files/zfbPayCert/"
    ];

    protected function initialize()
    {
        $options              = new Config();
        $options->protocol    = 'https';
        $options->gatewayHost = 'openapi.alipay.com';
        $options->signType    = 'RSA2';
        $options->appId       = self::CONFIG["appId"];

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        $options->merchantPrivateKey = '<-- 请填写您的应用私钥，例如：MIIEvQIBADANB ... ... -->';
        $options->alipayCertPath     = '<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->';
        $options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt" -->';
        $options->merchantCertPath   = '<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->';

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        // $options->alipayPublicKey = '<-- 请填写您的支付宝公钥，例如：MIIBIjANBg... -->';

        //可设置异步通知接收服务地址（可选）
        $options->notifyUrl = self::CONFIG["notifyUrl"];

        Factory::setOptions($options);
    }

    //获取授权访问令牌和用户 user_id
    public function getToken($code)
    {
        try {
            // 发起API调用）
            $result = Factory::Base()->OAuth()->getToken($code);
            $responseChecker = new ResponseChecker();

            // 处理响应或异常
            if ($responseChecker->success($result)) {
                echo "调用成功". PHP_EOL;
            } else {
                echo "调用失败，原因：". $result->msg."，".$result->subMsg.PHP_EOL;
            }
        } catch (\Exception $e) {
            echo "调用失败，". $e->getMessage(). PHP_EOL;;
        }
    }

    //刷新授权访问令牌
    public function refreshToken($refreshToken)
    {
        try {
            // 发起API调用）
            $result = Factory::Base()->OAuth()->refreshToken($refreshToken);
            $responseChecker = new ResponseChecker();

            // 处理响应或异常
            if ($responseChecker->success($result)) {
                echo "调用成功". PHP_EOL;
            } else {
                echo "调用失败，原因：". $result->msg."，".$result->subMsg.PHP_EOL;
            }
        } catch (\Exception $e) {
            echo "调用失败，". $e->getMessage(). PHP_EOL;;
        }
    }

    //创建小程序二维码
    public function createQrcode($urlParam, $queryParam, $describe)
    {
        try {
            // 发起API调用）
            $result = Factory::Base()->Qrcode()->create($urlParam, $queryParam, $describe);
            $responseChecker = new ResponseChecker();

            // 处理响应或异常
            if ($responseChecker->success($result)) {
                echo "调用成功". PHP_EOL;
            } else {
                echo "调用失败，原因：". $result->msg."，".$result->subMsg.PHP_EOL;
            }
        } catch (\Exception $e) {
            echo "调用失败，". $e->getMessage(). PHP_EOL;;
        }
    }

    //上传门店照片
    public function uploadImage()
    {
        $imageName     = "";
        $imageFilePath = "";
        try {
            // 发起API调用）
            $result = Factory::Base()->Image()->upload($imageName, $imageFilePath);
            $responseChecker = new ResponseChecker();

            // 处理响应或异常
            if ($responseChecker->success($result)) {
                echo "调用成功". PHP_EOL;
            } else {
                echo "调用失败，原因：". $result->msg."，".$result->subMsg.PHP_EOL;
            }
        } catch (\Exception $e) {
            echo "调用失败，". $e->getMessage(). PHP_EOL;;
        }
    }

    //上传门店视频
    public function uploadVideo()
    {
        $videoName     = "";
        $videoFilePath = "";
        try {
            // 发起API调用）
            $result = Factory::Base()->Video()->upload($videoName, $videoFilePath);
            $responseChecker = new ResponseChecker();

            // 处理响应或异常
            if ($responseChecker->success($result)) {
                echo "调用成功". PHP_EOL;
            } else {
                echo "调用失败，原因：". $result->msg."，".$result->subMsg.PHP_EOL;
            }
        } catch (\Exception $e) {
            echo "调用失败，". $e->getMessage(). PHP_EOL;;
        }
    }

    //创建交易
    public function paymentCreate()
    {
        $subject     = "iPhone6 16G";
        $outTradeNo  = "20200326235526001";
        $totalAmount = "88.88";
        $buyerId     = "2088002656718920";
        try {
            // 发起API调用
            $result = Factory::payment()->common()->create($subject, $outTradeNo, $totalAmount, $buyerId);
            $responseChecker = new ResponseChecker();

            // 处理响应或异常
            if ($responseChecker->success($result)) {
                echo "调用成功". PHP_EOL;
            } else {
                echo "调用失败，原因：". $result->msg."，".$result->subMsg.PHP_EOL;
            }
        } catch (\Exception $e) {
            echo "调用失败，". $e->getMessage(). PHP_EOL;;
        }
    }

    //查询交易
    public function paymentQuery()
    {
        $outTradeNo  = "20200326235526001";
        try {
            // 发起API调用
            $result = Factory::payment()->common()->query($outTradeNo);
            $responseChecker = new ResponseChecker();

            // 处理响应或异常
            if ($responseChecker->success($result)) {
                echo "调用成功". PHP_EOL;
            } else {
                echo "调用失败，原因：". $result->msg."，".$result->subMsg.PHP_EOL;
            }
        } catch (\Exception $e) {
            echo "调用失败，". $e->getMessage(). PHP_EOL;;
        }
    }

    //交易退款
    public function paymentRefund()
    {
        $outTradeNo   = "20200326235526001";
        $refundAmount = "88.88";
        try {
            // 发起API调用
            $result = Factory::payment()->common()->refund($outTradeNo, $refundAmount);
            $responseChecker = new ResponseChecker();

            // 处理响应或异常
            if ($responseChecker->success($result)) {
                echo "调用成功". PHP_EOL;
            } else {
                echo "调用失败，原因：". $result->msg."，".$result->subMsg.PHP_EOL;
            }
        } catch (\Exception $e) {
            echo "调用失败，". $e->getMessage(). PHP_EOL;;
        }
    }

    //交易退款查询
    public function paymentQueryRefund()
    {
        $outTradeNo   = "20200326235526001";
        $outRequestNo = "88.88";
        try {
            // 发起API调用
            $result = Factory::payment()->common()->queryRefund($outTradeNo, $outRequestNo);
            $responseChecker = new ResponseChecker();

            // 处理响应或异常
            if ($responseChecker->success($result)) {
                echo "调用成功". PHP_EOL;
            } else {
                echo "调用失败，原因：". $result->msg."，".$result->subMsg.PHP_EOL;
            }
        } catch (\Exception $e) {
            echo "调用失败，". $e->getMessage(). PHP_EOL;;
        }
    }

    //关闭交易
    public function paymentClose()
    {
        $outTradeNo   = "20200326235526001";
        try {
            // 发起API调用
            $result = Factory::payment()->common()->close($outTradeNo);
            $responseChecker = new ResponseChecker();

            // 处理响应或异常
            if ($responseChecker->success($result)) {
                echo "调用成功". PHP_EOL;
            } else {
                echo "调用失败，原因：". $result->msg."，".$result->subMsg.PHP_EOL;
            }
        } catch (\Exception $e) {
            echo "调用失败，". $e->getMessage(). PHP_EOL;;
        }
    }

    //异步通知验签
    public function verifyNotify()
    {

    }

}